import request from '@/utils/http'
import { BaseResult, PageParams, PageResult, PaginationResult } from '@/types/axios'
import {
  UserType,
  RoleType,
  DictType,
  BusinessPermissionType,
  UserUpdatePhoneParam,
  UserGetCodeParam,
  UserLoginParam
} from './model/setupModel'

export class SetupService {
  // *************用户管理*************
  // 用户管理查询
  static userGetAll(params: any) {
    return request.post<PaginationResult<UserType[]>>({
      url: '/platform_user/getAll',
      params
    })
  }

  // 新增账户
  static userAdd(params: UserType) {
    return request.post<BaseResult>({
      url: '/platform_user/save',
      params
    })
  }

  // 用户编辑查询
  static userGetById(params: { platformUserId: number }) {
    return request.post<BaseResult<UserType>>({
      url: '/platform_user/getById',
      params
    })
  }

  // 修改账户
  static userUpdate(params: UserType) {
    return request.post<BaseResult>({
      url: '/platform_user/update',
      params
    })
  }

  // 删除账户
  static userDelete(params: { id: number }) {
    return request.post<BaseResult>({
      url: '/platform_user/delete',
      params
    })
  }

  // *************角色管理*************
  // 角色管理查询
  static roleGetAll(params: any) {
    return request.post<PaginationResult<RoleType[]>>({
      url: '/role/getAll',
      params
    })
  }

  // 新增角色
  static roleAdd(params: RoleType) {
    return request.post<BaseResult>({
      url: '/role/save',
      params
    })
  }

  // 编辑角色
  static roleUpdate(params: RoleType) {
    return request.post<BaseResult>({
      url: '/role/update',
      params
    })
  }

  // 角色详情查询
  static roleGetById(params: { id: number }) {
    return request.post<BaseResult<RoleType>>({
      url: '/role/getById',
      params
    })
  }

  // 删除角色
  static roleDelete(params: { id: number }) {
    return request.post<BaseResult>({
      url: '/role/delete',
      params
    })
  }

  // 菜单分配查询
  static getRolePermission(params: { roleId: number }) {
    return request.post<BaseResult<any>>({
      url: '/role/getRolePermission',
      params
    })
  }

  // 菜单分配
  static distribution(params: { roleId: number; permissionIds: number[] }) {
    return request.post<BaseResult>({
      url: '/role/distribution',
      params
    })
  }

  // *************字典管理*************
  // 字典管理查询
  static dictGetAll(params: any) {
    return request.post<BaseResult<PageResult<DictType>>>({
      url: '/dict/getAll',
      params
    })
  }

  // 新增字典
  static dictAdd(params: DictType) {
    return request.post<BaseResult>({
      url: '/dict/save',
      params
    })
  }

  // 编辑字典
  static dictUpdate(params: DictType) {
    return request.post<BaseResult>({
      url: '/dict/update',
      params
    })
  }

  // 字典详情查询
  static dictGetById(params: { dictId: number }) {
    return request.post<BaseResult<DictType>>({
      url: '/dict/getById',
      params
    })
  }

  // 删除字典
  static dictDelete(params: { dictId: number }) {
    return request.post<BaseResult>({
      url: '/dict/delete',
      params
    })
  }

  // 根据父节点id查询子级字典数据
  static dictGetChilds(params: { dictPid: number } & PageParams) {
    return request.post<BaseResult<PageResult<DictType[]>>>({
      url: '/dict/getChilds',
      params
    })
  }

  // 字典下拉数据查询
  static dictGetSelect(params: any) {
    return request.post<BaseResult<DictType[]>>({
      url: '/dict/getSelect',
      params
    })
  }

  // *************菜单管理*************
  // 菜单管理查询
  static tabsGetAll(params: any) {
    return request.post<PaginationResult<any[]>>({
      url: '/permission/getAll',
      params
    })
  }

  // 添加菜单
  static tabsAdd(params: any) {
    return request.post<BaseResult>({
      url: '/permission/save',
      params
    })
  }

  // 编辑菜单查询
  static tabsGetById(params: { id: number }) {
    return request.post<BaseResult<any>>({
      url: '/permission/getById',
      params
    })
  }

  // 查询当前最大排序值
  static getMaxSort(params: any) {
    return request.post<BaseResult<number>>({
      url: '/permission/getMaxSort',
      params
    })
  }

  // 编辑菜单
  static tabsUpdate(params: any) {
    return request.post<BaseResult>({
      url: '/permission/update',
      params
    })
  }

  // 删除菜单
  static tabsDelete(params: { id: number }) {
    return request.post<BaseResult>({
      url: '/permission/delete',
      params
    })
  }

  // 同步菜单
  static businessSyncMenu(params: any) {
    return request.post<BaseResult>({
      url: '/businessPermission/syncMenu',
      timeout: 5000,
      params
    })
  }
}

// =======================商家菜单=====================
export class BusinessMenuService {
  // 商家菜单管理查询
  static getAll(params: any) {
    return request.post<BaseResult<PageResult<any[]>>>({
      url: '/businessPermission/getAll',
      params
    })
  }

  // 添加商家菜单
  static save(params: BusinessPermissionType) {
    return request.post<BaseResult>({
      url: '/businessPermission/save',
      params
    })
  }

  // 编辑商家菜单查询
  static getById(params: { permissionId: number }) {
    return request.post<BaseResult<BusinessPermissionType>>({
      url: '/businessPermission/getById',
      params
    })
  }

  // 查询当前最大排序值
  static getMaxSort() {
    return request.post<BaseResult<number>>({
      url: '/businessPermission/getMaxSort'
    })
  }

  // 编辑商家菜单
  static update(params: BusinessPermissionType) {
    return request.post<BaseResult>({
      url: '/businessPermission/update',
      params
    })
  }

  // 删除商家菜单
  static delete(params: { permissionId: number }) {
    return request.post<BaseResult>({
      url: '/businessPermission/delete',
      params
    })
  }
}

// =======================手机号管理=====================
export class PhoneService {
  /**
   * 获取管理员手机号
   */
  static getAdminPhone() {
    return request.post<BaseResult<string>>({
      url: '/admin/getAdminPhone'
    })
  }

  /**
   * 获取更新管理员手机号的短信验证码
   */
  static getUpdatePhoneCode(phone: string) {
    return request.post<BaseResult>({
      url: '/admin/getUpdatePhoneCode',
      data: {
        phone
      }
    })
  }

  /**
   * 更新用户手机号
   */
  static updatePhone(param: UserUpdatePhoneParam) {
    return request.post<BaseResult<boolean>>({
      url: '/platform_user/updatePhone',
      data: param
    })
  }

  /**
   * 获取隐私验证码
   */
  static getPrivacyCode(params: UserGetCodeParam) {
    return request.post<BaseResult<boolean>>({
      url: '/admin/getPrivacyCode',
      params
    })
  }

  /**
   * 验证二次隐私开启
   */
  static verifyPrivacyCode(params: UserLoginParam) {
    return request.post<BaseResult<boolean>>({
      url: '/admin/verifyPrivacyCode',
      params
    })
  }
}
